package com.zzk.rxcomponent.ui.splash;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPStaticUtils;
import com.zzk.baselibrary.global.SPKeyGlobal;
import com.zzk.baselibrary.router.RouteUtil;
import com.zzk.rxmvvmbase.base.BaseModel;
import com.zzk.rxmvvmbase.base.BaseViewModel;
import com.zzk.rxcomponent.R;

import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Function;
import io.reactivex.rxjava3.schedulers.Schedulers;

/**
 * @ProjectName: RXMVVMBaseComponent
 * @Package: com.zzk.rxcomponent.ui.splash
 * @ClassName: SplashViewModel
 * @Description:
 * @Author: brilliantzhao
 * @CreateDate: 2021.1.14 11:21
 * @UpdateUser:
 * @UpdateDate: 2021.1.14 11:21
 * @UpdateRemark:
 * @Version: 1.0.0
 */

public class SplashViewModel extends BaseViewModel {

    //##########################  custom variables start ##########################################

    public ObservableField<String> timeCountDown = new ObservableField<>("-");

    //##########################  custom variables end  ###########################################

    //##########################  override custom metohds start ###################################

    public SplashViewModel(@NonNull Application application) {
        super(application);
    }

    public SplashViewModel(@NonNull Application application, BaseModel model) {
        super(application, model);
    }

    //##########################  override custom metohds end  ####################################

    //##########################  override third methods start ####################################

    //##########################  override third methods end  #####################################

    //##########################  custom metohds start     ########################################

    /**
     * 页面倒计时处理
     *
     * @param totalTime
     */
    public void dealTimeCountDown(final int totalTime) {
        // 延迟一秒计时
        Observable.interval(1, TimeUnit.SECONDS)
                .take(totalTime)
                .map(new Function<Long, Long>() {
                    @Override
                    public Long apply(Long aLong) throws Exception {
                        //强制格式化一次剩余时间（延时 mTime 秒 最后倒计时结束显示 0 则需要多 减去 1）
                        return totalTime - 1 - aLong;
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        LogUtils.i("dealTimeCountDown->onSubscribe");
                        timeCountDown.set(totalTime + "  " + getApplication().getString(R.string.splash_skip));
                    }

                    @Override
                    public void onNext(Long value) {
                        LogUtils.i("dealTimeCountDown->onNext:" + value);
                        //处理剩余倒计时时间数据
                        timeCountDown.set(value + "  " + getApplication().getString(R.string.splash_skip));
                    }

                    @Override
                    public void onError(Throwable e) {
                        LogUtils.i("dealTimeCountDown->onError");
                    }

                    @Override
                    public void onComplete() {
                        LogUtils.i("dealTimeCountDown->onComplete");
                        // 倒计时结束所需要做的操作
                        // 判断是否需要展示引导页
                        if (SPStaticUtils.getBoolean(SPKeyGlobal.IS_GUIDE_DISPLAYED, false)) {
                            RouteUtil.startHomeActivity();
                        } else {
                            RouteUtil.startHomeActivity();
                        }
                        finish();
                    }
                });
    }

    //##########################  custom metohds end   ############################################

}
